import {action, observable} from "mobx";
import {Bubble, Tools} from "weaver-mobile";
import {Browser, BrowserHrm, ListViewPage} from "weaver-mobile-page";
import {accessAuditListApi, accessAuditOperationApi} from "../../api/performance/accessAuditApi";

const getLabel = Tools.getLabel;

/**
 * 考核评分
 * jiansong 2019/11/19
 */
export default class AccessAuditStore {

    @observable list = new ListViewPage.Store({route: "/performance/accessAudit"});
    @observable subBrowser = new Browser.Store({route: "/performance/accessAudit/condition"});
    @observable deptBrowser = new Browser.Store({route: "/performance/accessAudit/condition"});
    @observable hrmBrowser = new BrowserHrm.Store({route: "/performance/accessAudit/condition"});
    @observable subBrowserValue = [];
    @observable deptBrowserValue = [];
    @observable hrmBrowserValue = [];

    @observable queryParams = { //查询条件
        scorename: "", //标题
        showtype: 1, //类型
        showstatus: "", //考核状态，只有在考核选项卡中才有
        type1: "", //周期
        hrmids: "", //人员
        includesub: "3", //是否包含分部，默认含子分部
        subcompanyids: "", //分部
        includedept: "3", //是否包含分部，默认含子部门
        departmentids: "", //部门
    };

    @action("初始化操作")
    doInit = () => {
        this.queryParams = {
            ...this.queryParams,
            scorename: "",
            showstatus: "",
            type1: "",
            hrmids: "",
            includesub: "3",
            subcompanyids: "",
            includedept: "3",
            departmentids: "",
        }
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
        this.getTableDatas();
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.queryParams[key] = params[key];
        }
    }

    @action("重置条件")
    resetCondition = () => {
        this.queryParams = {
            ...this.queryParams,
            scorename: "",
            showstatus: "",
            type1: "",
            hrmids: "",
            includesub: "3",
            subcompanyids: "",
            includedept: "3",
            departmentids: "",
        };
        this.subBrowserValue = [];
        this.deptBrowserValue = [];
        this.hrmBrowserValue = [];
    }

    @action("更新store数据")
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            this[key] = params[key];
        });
    }

    @action("渲染table数据")
    getTableDatas = () => {
        accessAuditListApi(this.queryParams).then(action(res => {
            if (res.api_status) {
                this.list.clearData();
                const {datas: dataKey} = res;
                this.list.getData({dataKey});
            } else {
                Bubble.error(res.msg || getLabel(508818, "接口调用失败！"))
            }
        }));
    };

    @action("待考核评分操作")
    accessAuditOperation = (params) => {
        accessAuditOperationApi(params).then(action(res => {
            if (res.hasRight) {
                this.list.selectNone();
                this.list.clearData();
                this.list.getData();
                Bubble.success(getLabel('30700', '操作成功'))
                if (window.RefreshAccessNum && typeof window.RefreshAccessNum === "function") {
                    window.RefreshAccessNum();
                }
            } else {
                Bubble.error(getLabel(2012, "对不起，您暂时没有权限！"))
            }
        }));
    };

}